<?php


/* Class file for a coach. */

/*
  I load a coach from the database for a given team
  I may return false if a coach could not be loaded.
  Otherwise I return the coach instance
*/
function loadCoach($coach_id) {
    $rs = executeQuery("select coach_id, name, password, type, email from coaches where coach_id=$coach_id");
    if ($row = mysql_fetch_row($rs)) {
        return new Coach($row[0], $row[1], $row[2], $row[3], $row[4]);
    }
    return false;
}

/*
  I load all coaches from the database
*/
function loadCoaches() {
    $array = array ();
    $rs = executeQuery("select coach_id, name, password, type, email from coaches");
    while ($row = mysql_fetch_row($rs)) {
        $coach = new Coach($row[0], $row[1], $row[2], $row[3], $row[4]);
        $array[$coach->id] = $coach;
    }

    return $array;
}

/*
  I load a coach from the database given a coach name and password
  I may return false if a coach could not be loaded.
  Otherwise I return the coach instance
*/
function loginCoach($name, $password) {
    $rs = executeQuery("select coach_id, name, password, type, email from coaches where name='$name' and password='$password'");
    $row = mysql_fetch_row($rs);
    if ($row != false) {
        return new Coach($row[0], $row[1], $row[2], $row[3], $row[4]);
    } else {
        return false;
    }
}

class Coach {
    var $id;
    var $name;
    var $password;
    var $type;
    var $email;
    var $link;

    function Coach($id, $name, $password, $type, $email) {
        $this->id = $id;
        $this->name = $name;
        $this->password = $password;
        $this->type = $type;
        $this->email = $email;
        $this->link = "<a href=\"coach_details.php?id=$this->id\">$this->name</a>";
    }

    function isAdmin() {
        return $this->type != 0;
    }

    function canUpdateTeam($team) {
        global $season;
        if ($this->isAdmin())
            return true;
        if ($team->coach->id == $this->id) {
            return !$season->noTeamEdit || $team->currentSeasonStats->played == 0;
        }
        return false;
    }
    
    function hasEmail() {
        return strlen($this->email) > 0;
    }

    function canEditMatch($match) {
        if ($this->isAdmin())
            return true;
        if ($match->teamsUpdated)
            return false;
        global $teams;
        $homeTeam = $teams->getTeam($match->homeRec->teamId);
        $awayTeam = $teams->getTeam($match->awayRec->teamId);
        if ($homeTeam->coach->id == $this->id || $awayTeam->coach->id == $this->id) {
            return true;
        }
        return false;
    }
    
    function canEditComment($matchRecord) {
        if ($this->isAdmin())
            return true;
        global $teams;
        $team = $teams->getTeam($matchRecord->teamId);
        if ($team->coach->id == $this->id) {
            return true;
        }
        return false;
    }

    function getName() {
        return $this->link;
    }
}
?>